package com.lesson01;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

//练习Thread，实现多线程下载图片
public class TestThread02 extends Thread{
    private String url; //网络图片地址
    private String name; //保存的图片名称

    public TestThread02(String url,String name){
        super();
        this.url=url;
        this.name=name;
    }
    @Override
    public void run() {
        try {
            WebDownloader webDownloader = new WebDownloader();
            webDownloader.download(url, name);
            System.out.println("下载了文件名为：" + name);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        TestThread02 t1 = new TestThread02("https://tse4-mm.cn.bing.net/th/id/OIP-C.LYkFkGjO6lQpTIQa-7aZXQHaDJ?rs=1&pid=ImgDetMain","1.jpg");
        TestThread02 t2 = new TestThread02("https://tse4-mm.cn.bing.net/th/id/OIP-C.LYkFkGjO6lQpTIQa-7aZXQHaDJ?rs=1&pid=ImgDetMain","2.jpg");
        TestThread02 t3 = new TestThread02("https://tse4-mm.cn.bing.net/th/id/OIP-C.LYkFkGjO6lQpTIQa-7aZXQHaDJ?rs=1&pid=ImgDetMain","3.jpg");

        t1.start();
        t2.start();
        t3.start();
    }
}
//下载器
class WebDownloader{
    public void download(String url,String name) throws IOException {
        FileUtils.copyURLToFile(new URL(url),new File(name));
    }
}